from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from rag_pro11.rag.config import RagConfig

# 从配置获取MySQL连接信息
MYSQL_USER = RagConfig.mysql_user
MYSQL_PASSWORD = RagConfig.mysql_password
MYSQL_HOST = RagConfig.mysql_host
MYSQL_PORT = RagConfig.mysql_port
MYSQL_DB = RagConfig.mysql_db

# 构建连接字符串
DATABASE_URL = f"mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DB}"

# 创建引擎和会话
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

# 依赖项 - 获取数据库会话
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()